package com.security.browser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * ${DESCRIPTION}
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2017-11-23 10:08
 **/
@Component
public class MyUserDetailsService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登陆用户名 : {}", username);
        // 根据用户名查找用户信息

        // 加密步骤，用户注册时加密密码
        String password = passwordEncoder.encode("123456");
        logger.info("数据库密码 : {}", password);
        // 用户名，密码，权限
        User user =  new User(username, password,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        // 判断账户状态是否正常
        // username 用户名
        // password   密码
        // enabled  账户是否可以，true - 可用
        // accountNonExpired  账户未过期-true，账户过期-false
        // credentialsNonExpired  密码未过期-true，密码过期-false
        // accountNonLocked  用户账户未被锁定-true，用户账号被锁定-false
        // authorities 权限
      //  User(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority > authorities)
        return user;
    }


}
